#include "workCmd.h"

int makeSalt(char *salt){
    int i, flag;
    srand(time(NULL));
    salt[0] = '$';
    salt[1] = '6';
    salt[2] = '$';
    for(i = 0; i < SATL_LEN; ++i){
        flag = rand()%3;
        switch(flag){
        case 0:
            salt[i] = rand()%26 + 'a';
            break;
        case 1:
            salt[i] = rand()%26 + 'A';
            break;
        case 2:
            salt[i] = rand()%10 + '0';
            break;
        }
    }
    return 0;
}

int registerUser(trainState_t trainState,int netFd){
    char userName[100] = {0};
    char passwd[100] = {0};
    int userId = 0;
    int check = 0;

    strncpy(userName,trainState.buf,trainState.length);
    printf("userName = %s\n",userName);
    strcpy(passwd,trainState.buf + trainState.preLength);

    int ret = getUserId(userName,&userId);
    if(ret == 0){
        //查找成功，用户已经存在
        check = 2;
    }

    //发送盐值
    train_t train;
    char salt[SATL_LEN + 1] = {0};
    makeSalt(salt);
    train.length = strlen(salt);
    strcpy(train.data,salt);
    send(netFd,&train.length,sizeof(train.length),MSG_NOSIGNAL);
    send(netFd,train.data,train.length,MSG_NOSIGNAL);

    //收取密文
    int dataLength;
    char cryptPasswd[100] = {0};
    bzero(&train,sizeof(train));
    recvn(netFd,&dataLength,sizeof(dataLength));
    recvn(netFd,cryptPasswd,dataLength);

    char pwd[100] = "~/";

    if(check == 2){
        //有这个用户了，发送回执
        send(netFd,&check,sizeof(int),MSG_NOSIGNAL);
        return 0;
    }

    //注册
    MYSQL *conn = NULL;
    ret = mysqlConnect(&conn);
    if(ret != 0){
        //注册失败
        check = 1;
    }
    ret = mysql_query(conn,"set names 'utf8'");
    if(ret != 0){
        printf("Error query: %s\n",mysql_error(conn));
        return EXIT_FAILURE;
    }

    char query[1024] = {0};
    sprintf(query,"insert into clientInfo(userName,salt,cryptPasswd,pwd,code)\
           values('%s','%s','%s','%s',0) ",
           userName,salt,cryptPasswd,pwd);
    ret = doQuery(query,&conn);
    if(ret != 0){
        //插入失败
        check = 1;
    }
    mysql_close(conn);

    //日志记录
    if(check == 0){
        getUserId(userName,&userId);
        sysadd(userId,"registed");
    }

    //发送注册状态回执
    send(netFd,&check,sizeof(int),MSG_NOSIGNAL);
}

int login(trainState_t trainState,int netFd,clientQueue_t *pclientQueue){
    char userName[100] = {0};
    char token[1024] = {0};
    char key[100] = {0};
    int userId = 0;
    int check = 0;

    strncpy(userName,trainState.buf,trainState.length);
    printf("userName = %s\n",userName);

    //获取盐值和密文
    char cryptPasswd[100] = {0};
    char salt[SATL_LEN + 1] = {0};
    int ret = getUserSalt(userName,salt,cryptPasswd);
    //查找成功
    if(ret == 0){
        //发送回执
        check = 0;
        send(netFd,&check,sizeof(check),MSG_NOSIGNAL);

        //发送盐值
        train_t train;
        bzero(&train,sizeof(train));
        train.length = strlen(salt);
        strcpy(train.data,salt);
        send(netFd,&train.length,sizeof(train.length),MSG_NOSIGNAL);
        send(netFd,train.data,train.length,MSG_NOSIGNAL);

        //接收密文
        int dataLength;
        char cryptPasswdClient[100] = {0};
        bzero(&train,sizeof(train));
        recvn(netFd,&dataLength,sizeof(dataLength));
        recvn(netFd,cryptPasswdClient,dataLength);

        if(strncmp(cryptPasswd,cryptPasswdClient,49) != 0){
            check = 1;//密码错误
        }
        //发送验证结果
        send(netFd,&check,sizeof(check),MSG_NOSIGNAL);
        if(check == 1){
            return EXIT_FAILURE;
        }
        else{
            //找到，加入连接队列
            printf("Passwd is right\n");
            getUserId(userName,&userId);
            sprintf(key,"YoUR sUpEr S3krEt %d HMAC kEy HeRE",userId);
            pclientQueue->clientFd[netFd] = userId;

            sysadd(userId,"login successful!");

            encode(key,userName,token);
        }
    }
    else{
        //查找用户到未注册
        check = 2;
        send(netFd,&check,sizeof(int),MSG_NOSIGNAL);
    }
    if(check == 0){
        //登陆成功，发送token\key\Dir
        train_t train;
        bzero(&train,sizeof(train));
        train.length = strlen(token);
        strcpy(train.data,token);
        send(netFd,&train,sizeof(train.length) + train.length,MSG_NOSIGNAL);

        bzero(&train,sizeof(train));
        train.length = strlen(key);
        strcpy(train.data,key);
        send(netFd,&train,sizeof(train.length) + train.length,MSG_NOSIGNAL);

        bzero(&train,sizeof(train));
        char pwd[100] = "~/";
        train.length = strlen(pwd);
        strcpy(train.data,pwd);
        send(netFd,&train,sizeof(train.length) + train.length,MSG_NOSIGNAL);
    } 
    return check;
}

int token(trainState_t trainState,int netFd){
    int check = 1;
    int userId = -1;
    char token[1024] = {0};
    char key[100] = {0};
    char userName[100] = {0};
    strncpy(userName,trainState.buf,trainState.preLength);
    strcpy(token,trainState.buf + trainState.preLength);

    getUserId(userName,&userId);
    sprintf(key,"YoUR sUpEr S3krEt %d HMAC kEy HeRE",userId);

    int ret = decode(key,userName,token);
    if(ret == 0){
        printf("Token right!\n");
        check = 0;
    }
    else{
        //不正确id变成-1
        userId = -1;
    }
    send(netFd,&check,sizeof(int),MSG_NOSIGNAL);
    return userId;
}



